package com.nowcoder.community.util;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName CookieUtil
 * @Description
 * @Author hhwu
 * @Date 2022/5/20 12:57
 * Version 1.0
 **/
public class CookieUtil {
    public static String getValue(HttpServletRequest request, String name) {
        /**
         * @Author hhwu
         * @Description //TODO 查询request中是否携带key为name的cookie，有的话返回其value
         * @Date 13:08 2022/5/20
         * @Param [request, name]
         * @return java.lang.String
         **/
        if (request == null || name == null) {
            throw new IllegalArgumentException("参数为空！");
        }

        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
//                System.out.println(cookie.getName()+":"+name);
                if (cookie.getName().equals(name)) {// 一定注意字符串比较相等时用.equals()!!!
                    return cookie.getValue();
                }
            }
        }
        return null;
    }
}
